var createmask=function(i,a,d)
{
	if (d==1) {
		return function (o) 
		{ var m=i,x=Math.max(o-a,m); 
		  this.style.width=x+'px';if (this.invoke) this.invoke(); 
		  ElementResizeListener.onchange(m==x); } 
	}
	return function (o) 
	{ var m=i,x=Math.max(window_w() - docx(this) - 40,m);  
	  this.style.width=x+'px';if (this.invoke) this.invoke();  } 
}

var foo=function ()
{ 
	var u,w=this.offsetWidth,h=this.offsetHeight,v=this.value;
	if (w>0&&v.length>0)
	{
		v=v.replace (/&/g,'').replace (/'/g,'');
		u=LOCATION_PATH_NAME + '/bevel/width/'+w+'/height/'+h+'/combo/'+escape(v)+''; 
		this.style.backgroundImage='url('+u+')';
		this.value = ''; 
	}
}

var ElementResizeListener = {
	state		: 0,
	elements 	: {},
	add 		: function (that, name)
	{
		if (!that) return false;
        window.resizer.subscribe (that);
		this.elements[name] = that;
		if (this.mask[name])  this.elements[name].zoom  = this.mask[name];
//		if (this.combo[name]) 
//		{
//			this.elements[name].combo = this.combo[name];
//			this.elements[name].onkeydown = function ()
//			{
//				this.style.backgroundImage=null;
//			};
//		}
	},
	clear 		: function()
	{
		this.elements = {};
	},
	onchange	: function(toosmall)
	{
		this.state = toosmall ? 2 : 1;
		
	},
	invoke 		: function()
	{
		var dc=document.getElementById ('dcevent');
		if (dc)
		{
			for (var x in this.elements)
			{ 
				if (this.elements[x].zoom)
					this.elements[x].zoom (dc.offsetWidth); 
				if (this.elements[x].combo)
					this.elements[x].combo (); 
			}
		}
	},
	combo : {
		EventDate : foo,
		//RoomName : foo,
		EventStartTime : foo,
		EventEndTime : foo,
		SetupStartTime : foo,
		SetupEndTime : foo,
		RecurseType : foo,
		RecurseEndDate : foo
	},
	mask : {
		EventName 			: createmask (300, 400, 1),/*function (o) { var m=300,x=Math.max(o-400,m);this.style.width=m+'px'; }*/ 
		Comments 			: createmask (300, 425, 1),//function (o) { this.style.width = Math.max(o-425,300) + 'px'; }, 
		RoomName 			: createmask (300, 400, 1),//function (o) { this.style.width = Math.max(o-400,300) + 'px'; },
		panel__comments		: createmask (375, 330, 1),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		panel__attachments	: createmask (375, 330, 1),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		property__main		: createmask (375, 330, 1),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		panel__options		: createmask (200, 200, 2),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		panel_clone			: createmask (200, 200, 2),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		panel_conf			: createmask (200, 200, 2),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		panel__properties	: createmask (200, 200, 2),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		panel__categories	: createmask (200, 200, 2),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		panel__calendars	: createmask (200, 200, 2),//function (o) { this.style.width = Math.max(o-330,375) + 'px'; this.invoke() },
		iComments			: createmask (200, 530, 1),//function (o) { this.style.width = Math.max(o-530,200) + 'px'; this.invoke() } ,
		deventdateremove	: createmask (300, 425, 1)//function (o) { this.style.width = Math.max(o-425,300) + 'px'; }
	}
}

function AttachEventFormElements ()
{ 
	var da=document.getElementsByTagName ('INPUT');
	var dt=document.getElementsByTagName ('TEXTAREA');
	var db=document.getElementsByTagName ('TABLE');
	
	
	ElementResizeListener.clear ();
	AttachEventFormElementsEx (da);
	AttachEventFormElementsEx (dt); 
	LoadRoomCheckList ();
	
    var dz=document.getElementById ('dcal');
    if (dz)dz.style.display='inline';
	
	var dc=document.getElementById ('dcevent');  
	window.resizer.clear ();
	if (dc)
	{
		dc.invoke = function ()
		{
			this.style.height = window_h() - docy (this) - 10;
			this.style.width = window_w() - 176;
		}
		window.resizer.subscribe (dc);
		dc.invoke();
	} 
	
	var extra=['iComments','deventdateremove']
	for (var x in extra) ElementResizeListener.add (document.getElementById (extra[x]), extra[x]);
	window.resizer.subscribe (ElementResizeListener);
	
	var name, bevel = {
		blue : {
			band 	: 'BCBCDD',
			color 	: 'D0D0EE',
			size 	: 6
		},
		pink : {
			band 	: 'DD9999',
			color 	: 'FFE0E0',
			size 	: 6
		},
		green : {
			band 	: 'D2E6D2',
			color 	: 'D2E6D2',
			size 	: 10
		},
		gray  : {
			band 	: 'D4D0C8',
			color 	: 'EEEEF0',
			size 	: 6
		},
		create : function (width, height, name)
		{
			var f=this[name];
			return LOCATION_PATH_NAME + '/bevel/' + 
				'width/' + width + 
				'/height/' + height + 
				'/bevel/' + f.size + 
				'/band/' + f.band + 
				'/color/' + f.color + '';
		}
	}
	var hue;
	for (var x=0;x<db.length;x++)
	{
		if (db[x].className.indexOf ('bevel')==0)
		{
			name = db[x].className.substr (5);
			if (name.substr(0,1)=='x')
			{
				db[x].invoke = function ()
				{ 
					var color = this.className.substr (6);
					var url = LOCATION_PATH_NAME + '/bevel/' + 
						'width/' + this.offsetWidth + 
						'/height/' + this.offsetHeight + 
						'/bevel/10' + 
						'/band/' + color + 
						'/color/' + color + '';
					this.style.background = '#' + color + ' url(' + url + ') no-repeat'; 
				} 
				ElementResizeListener.add (db[x], db[x].id);
				db[x].invoke();
			}
			else if (bevel[ name ])
			{  
				db[x].invoke = function ()
				{ 
					var name = this.className.substr (5);
					this.style.background = '#' + bevel[ name ].color + ' url(' + bevel.create (this.offsetWidth, this.offsetHeight, name) + ') no-repeat'; 
				} 
				ElementResizeListener.add (db[x], db[x].id);
				db[x].invoke();
			}
		} 
	}
	window.resizer.invoke ();
}

function DataBoundInput () { }
DataBoundInput.onChange = function () 
{    
	if (this.type=='checkbox') window.cachevalue = this.checked ? this.value : 0; 
	else window.cachevalue = this.value;
	eval( 'window.currentevent.' + this.getAttribute ('datasrc') + '=window.cachevalue;' ); 
	OnDataSourceDateUpdate (this.getAttribute ('datasrc'));  
	window.resizer.invoke();
}
	
DataBoundInput.onUpdate = function (value, text) 
{    
	this.value = text||value;
	window.cachevalue = value;
	eval( 'window.currentevent.' + this.getAttribute ('datasrc') + '=window.cachevalue;' );  
	OnDataSourceDateUpdate (this.getAttribute ('datasrc')); 
	var re, ds; 
	if (re=/iEvent(\w+)Time/.exec (this.id))
	{
		ds=document.getElementById ('iSetup' + re[1] + 'Time');
		if (ds) ds.update (this.value);
	}   
	window.resizer.invoke();

}
	
DataBoundInput.onDateFocus = function ()
{
	HideTimeSelector();
	
	var oDate=new Date (this.value);
	if (isNaN(oDate)) oDate=new Date();
	
	var INPUT=this.id;
	window.eventdatepicker.onupdate=function () {}
	window.eventdatepicker.oDate=new Date (oDate.getFullYear(), oDate.getMonth(), 1);
	window.eventdatepicker.update ();
	window.eventdatepicker.setdate (oDate.getDate());
	window.eventdatepicker.onupdate=function ()
	{
		if (document.getElementById (INPUT))
		var value = this.Year+'-'+ (this.Month + 1) +'-'+this.Day;
		document.getElementById (INPUT).update ( value, dateInput(value) ); 
		window.eventdatepicker.onupdate=function () {}
		HideTimeSelector(); 
	} 
	this.select();
	var ds=document.getElementById ('ddateselect'); 
	ds.style.display ='inline'; 
	ds.style.left 	 = docx (this);
	ds.style.top 	 = docy(this) + this.offsetHeight;
}
DataBoundInput.onRangeFocus = function () 
{    
	HideTimeSelector();
	var types=new Array ("Daily","Weekly","Monthly","Monthly","Yearly","On Weekdays","","Custom...");
	
	var htm='<select multiple size=5 ';
	htm += ' onchange="';
	htm += ' document.getElementById (\'' + this.id + '\').update(this.options[this.selectedIndex].value,this.options[this.selectedIndex].text);';
	htm += ' HideTimeSelector();';
	htm += ' DebugEventDetailForm.invoke();';
	htm += ' " ';
	htm += ' style="width:' + this.offsetWidth + 'px">';
	htm += '<option value="-1" '+(window.currentevent.RecurseType<0?' selected ':'')+'>Does not repeat';
	for (var x in types)
	{
	    htm += '<option value="'+x+'" '+(x==window.currentevent.RecurseType?' selected ':'')+'>' + types[x];
	}
	
	htm += '</select>';
	var ds=document.getElementById ('dtimeselect');
	ds.innerHTML=htm;
	ds.style.display='inline';
	ds.style.left = docx (this);
	ds.style.top = docy(this) + this.offsetHeight;
}


function AttachEventFormElementsEx (da)
{ 
	//var da=document.getElementsByTagName ('INPUT');
	for (var x=0;x<da.length;x++)
	{
		da[x].invoke = function (x) { this.disabled = x; }
		window.disabler.subscribe (da[x]);
		
		var datasrc = da[x].getAttribute ('datasrc');
		if (datasrc)
		{
			ElementResizeListener.add (da[x], datasrc);
			da[x].onchange = DataBoundInput.onChange; 
			da[x].update   = DataBoundInput.onUpdate;  
			if (da[x].type=='checkbox') da[x].onclick = DataBoundInput.onChange;
			else if (da[x].className=='rangeselect') { da[x].onfocus = DataBoundInput.onRangeFocus; }
			else if (da[x].className=='dateselect') { da[x].onfocus = DataBoundInput.onDateFocus; } 
			else if (da[x].className=='dateadder')
			{ 
				da[x].onclick = function ()
				{
					var oDate = new Date();
					for (var m in window.currentevent.CustomDates)
					{
						oDate=new Date(window.currentevent.CustomDates[m].title);
						break;
					}
					window.eventdatepicker.oDate=new Date (oDate.getFullYear(), oDate.getMonth(), 1);
					window.eventdatepicker.update ();
					window.eventdatepicker.setdate (oDate.getDate());
					window.eventdatepicker.onupdate=function ()
					{
						var value = this.Year+'-'+ (this.Month + 1) +'-'+this.Day;
						var name = dateInput (value);
						window.currentevent.AddDate (name, value); 
						window.eventdatepicker.onupdate=function () {}
						HideTimeSelector();
						DebugEventDetailForm.invoke ();
					} 
					
					var ds=document.getElementById ('ddateselect'); 
					var dc=document.getElementById ('dcevent'); 
					ds.style.display = 'inline'; 
					ds.style.left 	 = docx (this);
					ds.style.top 	 = docy (this) + this.offsetHeight - dc.scrollTop;
				}
			} 
			else if (da[x].className=='roomselect')
			{
			    AttachRoomSelector (da[x]);
			} 
			else if (da[x].className=='timeselect')
			{ 
				da[x].onfocus = function ()
				{
					HideTimeSelector();
					var am;
					var h;
					var text, value;
					var f=function (x) { return parseInt(x,10) < 10 ? ('0' + x) : x; }
					var htm='<select multiple size=5 ';
					htm += ' onchange="document.getElementById (\'' + this.id + '\').update(this.value);HideTimeSelector();" ';
					htm += '>';
					for (var ap=0,his=0;ap<2;ap++)
					{
						am=ap==0?'am':'pm';
						for (var hh=0;hh<12;hh++)
						{
							h=hh==0?12:hh;
							for (var mm=0;mm<60;mm+=15)
							{
								text  = h + ':' + f(mm) + '' + am;
								htm += '<option value="' + text + '" '+(text==this.value?' selected ':'')+'>' + text;
							}
						}
						his++;
					}
					htm += '</select>';
					this.select();
					var re,ds=document.getElementById ('dtimeselect');
					var dc=document.getElementById ('dcevent'); 
					ds.innerHTML=htm;
					ds.style.display	= 'inline';
					ds.style.left 		= docx (this);
					ds.style.top 		= docy (this) + this.offsetHeight - dc.scrollTop;
				}
			}
			else
			{
				da[x].onfocus = HideTimeSelector;
			}
		}
	}
}

function OnDataSourceDateUpdate(datasrc)
{
	var dom=window.currentevent;
	var dates=dom.CustomDates;
	var type=dom.RecurseType; 
	if (datasrc=="EventDate"||datasrc=="RecurseEndDate"||datasrc=="RecurseType"||datasrc=="RecurseUnit")
	{
		if (type > Calendar.NOREPEAT)
		{
			if (dates.length==0)
			{
				MakeDateScope ()
				return true;
			}
			
			if (confirm ('Update your date list?'))
			{
				MakeDateScope ()
				return true;
			}
		}
		else
		{
			if (dates.length>0)
			{
				dom.CustomDates = new Array ();
				DebugEventDetailForm.invoke();
				return true;
			} 
		}
	}
}

function AttachRoomSelector (element)
{
    if (!element) return false;
	element.onfocus = function ()
	{
		HideTimeSelector();
		var ds=document.getElementById ('droomselect'); 
		ds.style.display ='inline';
		ds.style.width 	 = Math.max(this.offsetWidth, 300);
		ds.style.left 	 = docx (this);
		ds.style.top 	 = docy(this) + this.offsetHeight;
	}
}
    
//document.onkeyup = function (e)
//{
//	var ev=e||window.event;
//	confirm (e.which)
//}